# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

import sys

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        def traverse(root):
            if (not root.left) and (not root.right): return 1
            ldepth = traverse(root.left) if root.left else sys.maxsize
            rdepth = traverse(root.right) if root.right else sys.maxsize
            return min(ldepth, rdepth)+1
        return traverse(root) if root else 0
